
const global = getApp().globalData;
const Api = global.Api;
const wxApi = global.wxApi;

Page({
  data: {
    lessons: [],
    loadmoreType: 0,
    topicTotalNum: 0,
    lesson: true,
    subed: '1',
    focusViewId: '',
    screenH: ''
  },
  onLoad(options) {
    wxApi.setNavTitle('知识点列表');
    this.init(options);
    this.showTime = 1;
  },
  onShow() {
    if (this.showTime > 1) {
      wx.hideNavigationBarLoading();
    } else {
      this.showTime ++;
    }
  },
  onHide() {
    wx.hideNavigationBarLoading();
  },
  init(options) {
    
    this.varsInit(options);
    if (this.options.type === 'topic') {
      let topic = this.options;
      this.skipToTopicDetail(topic);
    }
    this.refresh();
  },
  varsInit(options) {
    this.pageNo = 1;
    this.topics = [];
    this.topicAPI = global.topicApi;
    this.logger = global.logger;
    this.unitId = options.unitId;
    this.lessonId = options.lessonId;
    this.viewId = options.viewId;
    this.setData({
      subed: JSON.parse(options.subed)
    })
    wxApi.setNavTitle(options.unitName);
    wxApi.getScreenSize(size => {
      this.setData({
        screenH: size.height
      })
    })
  },
  onReady() {
    
  },
  refresh() {
    this.pageNo = 1;
    let self = this;
    this.loadTopics(this.pageNo, topics => {
      this.setTopics( topics );
      wx.hideNavigationBarLoading();
      let timer = setTimeout(() => {
        self.setData({
          focusViewId: self.viewId
        })
        clearTimeout(timer);
        timer = null;
      }, 100);
    });
  },
  onReachBottom() {
    this.loadMore();
  },
  skipToTopicDetail(topic) {
    let params = {
      topic: wxApi.encodeURI(topic)
    }
    wxApi.navigateTo('../topic-detail/topic-detail', params);
  },
  onTopicClick(e) {
    let topic = e.detail;
    let params = {
      topic: wxApi.encodeURI(topic)
    }
    wxApi.navigateTo('../topic-detail/topic-detail', params);
  },
  setTopics( topics ) {
    this.topics = topics;
    let lessons = this.topicsParser(topics);
    this.logger.info("解析后课文列表", lessons);
    this.setData({
      lessons: lessons
    })
  },
  setLoadmoreType( val ) {
    this.setData({
      loadmoreType: val
    })
  },
  loadMore(fn) {
    // 显示加载更多
    this.setLoadmoreType( 1 );
    this.pageNo ++;
    let totalTopics = this.topics;
    this.loadTopics( this.pageNo, topics => {
      if ( !topics.length ) {
        // 显示没有更多
        this.pageNo --;
        this.setLoadmoreType( 2 );
        fn && fn(false) // 无更多
        return;
      }
      this.setLoadmoreType( 0 )
      totalTopics = totalTopics.concat( topics );
      this.setTopics( totalTopics );
      fn && fn(true) // 有更多
    } )
  },
  loadTopics(pageNo, callback) {
    let params = {
      pageNo: pageNo
    }
    this.topicAPI.getTopicsByUnitID(this.unitId, params, data => {
      let topics = data.records;
      this.logger.info('知识点列表', topics)
      this.topicTotalNum = data.total;
      callback && callback( topics );
    });
  },
  topicsParser(topics) {
    let lessonsObj = {}
    const testingTypeMap = {
      "0": "未知",
      "1": "常考",
      "2": "必考"
    }
    for (let i = 0, len = topics.length; i < len; i++) {
      let topic = topics[i];
      let lessonId = topic.lessonId;
      if (!lessonsObj[lessonId]) {
        lessonsObj[lessonId] = {
          id: lessonId,
          name: topic.lessonName,
          number: topic.lessonNumber,
          topics: []
        }
      }
      topic.index = i;
      topic.testingText = testingTypeMap[topic.testingType];
      lessonsObj[lessonId].topics.push(topic)
    }
    let lessons = [];
    for (let key in lessonsObj) {
      lessons.push(lessonsObj[key]);
    }
    return lessons;
  }

}) 